<?php

namespace app\admin\controller;

use app\admin\model\LangClassFile;
use app\admin\model\LangClassReciteAnswer;
use app\common\controller\Send;
use app\admin\model\LangClassRecite AS ReciteModel;
use app\admin\validate\LangClassRecite AS ReciteValidate;
use app\common\enums\StatusEnum;
use think\facade\Request;

class LangClassRecite extends Base
{
    use Send;

    /**
     * 背诵单词或者句子列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input();
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : ReciteModel::TYPE_WORD;
        $name = isset($search['word']) ? $search['word'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $typeId = isset($search['typeId']) ? $search['typeId'] : 0;
        $search = [
            'lang' => $lang,
            'className' => $className,
            'classId' => $classId,
            'courseType' => $courseType,
            'type' => $type,
            'typeId' => $typeId,
            'word' => $name,
            'status' => $status
        ];

        $where = [];
        if ($search['word']) {
            $where[] = ['word', 'like', '%' . $search['word'] . '%'];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = ReciteModel::getList($classId, $courseType, $type, $typeId, $where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'typeName' => ReciteModel::attributes('type', $type),
            'courseName' => LangClassFile::attributes('course_type', $courseType),
        ]);
    }

    /**
     * 添加修改单词或者句子
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');
            //print_r($data);exit()
            if (!isset($data['answer']) || count($data['answer']) <= 0) {
                return json(self::sendResult(102, [], '请填写答案'));
            }

            if (empty($data['file'])) {
                return json(self::sendResult(102, [], '请上传声音文件'));
            }

            foreach ($data['answer'] as $key => $value) {
                if (empty($value['image'])) {
                    return json(self::sendResult(102, [], '请上传所有图片'));
                }
            }
            
            // 检测变量
            $validate = new ReciteValidate();
            //var_dump($validate->check($data));
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }
            
            $reciteId = ReciteModel::saveOne($data);
            $code = $reciteId > 0 ? self::$okCode : 602;
            return json(self::sendResult($code, [
                'url' => url('lang_class_recite/index', [
                    'classId' => $data['class_id'],
                    'courseType' => $data['course_type'],
                    'type' => $data['type'],
                    'typeId' => $data['type_id'],
                    'lang' => $data['lang'],
                    'className' => $data['className'],
                ]),
            ]));
        } else {
            $search = input();
            $lang = isset($search['lang']) ? $search['lang'] : '';
            $className = isset($search['className']) ? $search['className'] : '';
            $classId = isset($search['classId']) ? $search['classId'] : 0;
            $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
            $type = isset($search['type']) ? $search['type'] : ReciteModel::TYPE_WORD;
            $typeId = isset($search['typeId']) ? $search['typeId'] : 0;
            $id = isset($search['id']) ? $search['id'] : 0;
            
            $model = [];
            $title = '添加';
            if ($id > 0) {
                $title = '编辑';
                $model = ReciteModel::get($id);
            }
            $answers = LangClassReciteAnswer::getListByRecite($id);
            return view('form', [
                'title' => $title,
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'typeId' => $typeId,
                'lang' => $lang,
                'className' => $className,
                'courseName' => LangClassFile::attributes('course_type', $courseType),
                'typeName' => ReciteModel::attributes('type', $type),
                'answers' => $answers,
            ]);
        }
    }

    /**
     * 删除数据
     * @param $id
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($id)
    {
        $model = ReciteModel::get($id);
        $lang = input('lang');
        $className = input('className');
        $classId = $courseType = $type = $typeId = 0;
        if ($model) {
            $reciteId = $model['id'];
            $classId = $model['class_id'];
            $courseType = $model['course_type'];
            $type = $model['type'];
            $typeId = $model['type_id'];

            // 删除单词或者句子
            $model->delete();

            // 删除下面的答案
            LangClassReciteAnswer::deleteData($reciteId);
        }

        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'typeId' => $typeId,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    public function deletes()
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : ReciteModel::TYPE_WORD;
        $typeId = isset($search['typeId']) ? $search['typeId'] : 0;
        $ids = $search['ids'];

        // 删除数据
        ReciteModel::deleteAll($ids);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'typeId' => $typeId,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        $model = ReciteModel::get($id);
        $classId = $courseType = $type = $typeId = 0;
        $lang = input('lang');
        $className = input('className');
        if ($model) {
            $classId = $model['class_id'];
            $courseType = $model['course_type'];
            $type = $model['type'];
            $typeId = $model['type_id'];

            $model->status = ($model->status == StatusEnum::ENABLED) ? StatusEnum::DISABLED : StatusEnum::ENABLED;
            $model->update_time = time();
            $model->save();
        }
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'typeId' => $typeId,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 批量启用、禁用
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disables()
    {
        $search = input();
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : 0;
        $typeId = $search['typeId'];

        $status = $search['status'];
        $ids = $search['ids'];
        ReciteModel::disables($ids, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'typeId' => $typeId,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }
}
